package org.csu.mypetstoreback.controller;

import org.csu.mypetstoreback.domain.Order;
import org.csu.mypetstoreback.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@Controller
@SessionAttributes("order")
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private Order order;


    @GetMapping("/order/allOrder")
    public String viewAllOrder(Model model){
        List<Order> orderList = orderService.getOrders();
        List<Order> waitOrderList = orderService.getWaitOrders();
        List<Order> deliveredOrderList = orderService.getDeliveredOrders();
        model.addAttribute("orderList", orderList);
        model.addAttribute("waitOrderList", waitOrderList);
        model.addAttribute("deliveredOrderList", deliveredOrderList);
        return "order/allOrder";
    }

    @GetMapping("/order/waitOrder")
    public String viewWaitOrder(Model model){
        List<Order> orderList = orderService.getOrders();
        List<Order> waitOrderList = orderService.getWaitOrders();
        List<Order> deliveredOrderList = orderService.getDeliveredOrders();
        model.addAttribute("orderList", orderList);
        model.addAttribute("waitOrderList", waitOrderList);
        model.addAttribute("deliveredOrderList", deliveredOrderList);
        return "order/waitOrder";
    }

    @GetMapping("/order/deliveredOrder")
    public String viewDeliveredOrder(Model model){
        List<Order> orderList = orderService.getOrders();
        List<Order> waitOrderList = orderService.getWaitOrders();
        List<Order> deliveredOrderList = orderService.getDeliveredOrders();
        model.addAttribute("orderList", orderList);
        model.addAttribute("waitOrderList", waitOrderList);
        model.addAttribute("deliveredOrderList", deliveredOrderList);
        return "order/deliveredOrder";
    }
/*
    @RequestMapping("/order/page")
    public String page(@RequestParam(defaultValue = "1") int pageNum,
                       @RequestParam(defaultValue = "5") int pageSize,
                       Model model){
        PageInfo<Order> pageInfo = orderService.findByPage(pageNum,pageSize);
        model.addAttribute("pageInfo",pageInfo);
        return "/order/list";
    }
    */

    @RequestMapping(value ="/order/deleteOrder1")
    @ResponseBody
    public String deleteOrder1(String orderId,Model model) throws IOException
    {
        //在数据库里删除一行订单信息
        //ajax传来的字符串"orderId"
        int a = Integer.parseInt(orderId);
        orderService.deleteOrder(a);
        List<Order> orderList = orderService.getOrders();
        List<Order> waitOrderList = orderService.getWaitOrders();
        List<Order> deliveredOrderList = orderService.getDeliveredOrders();
        model.addAttribute("orderList", orderList);
        model.addAttribute("waitOrderList", waitOrderList);
        model.addAttribute("deliveredOrderList", deliveredOrderList);
        return "/order/allOrder";
    }

    @RequestMapping(value ="/order/deleteOrder2")
    @ResponseBody
    public String deleteOrder2(String orderId,Model model) throws IOException
    {
        //在数据库里删除一行订单信息
        //ajax传来的字符串"orderId"
        int a = Integer.parseInt(orderId);
        orderService.deleteOrder(a);
        List<Order> orderList = orderService.getOrders();
        List<Order> waitOrderList = orderService.getWaitOrders();
        List<Order> deliveredOrderList = orderService.getDeliveredOrders();
        model.addAttribute("orderList", orderList);
        model.addAttribute("waitOrderList", waitOrderList);
        model.addAttribute("deliveredOrderList", deliveredOrderList);
        return "/order/waitOrder";
    }

    @RequestMapping(value ="/order/deleteOrder3")
    @ResponseBody
    public String deleteOrder3(String orderId,Model model) throws IOException
    {
        //在数据库里删除一行订单信息
        //ajax传来的字符串"orderId"
        int a = Integer.parseInt(orderId);
        orderService.deleteOrder(a);
        List<Order> orderList = orderService.getOrders();
        List<Order> waitOrderList = orderService.getWaitOrders();
        List<Order> deliveredOrderList = orderService.getDeliveredOrders();
        model.addAttribute("orderList", orderList);
        model.addAttribute("waitOrderList", waitOrderList);
        model.addAttribute("deliveredOrderList", deliveredOrderList);
        return "/order/deliveredOrder";
    }

    //订单发货
    @RequestMapping("/order/toDeliver")
    @ResponseBody
    public String toDeliver(@RequestBody Order order,Model model){
        //前端返回order
        //修改订单状态
        orderService.updateOrder(order);
        List<Order> orderList = orderService.getOrders();
        List<Order> waitOrderList = orderService.getWaitOrders();
        List<Order> deliveredOrderList = orderService.getDeliveredOrders();
        model.addAttribute("orderList", orderList);
        model.addAttribute("waitOrderList", waitOrderList);
        model.addAttribute("deliveredOrderList", deliveredOrderList);
        return "/order/toDeliver";
    }
    //修改订单
    @RequestMapping("/order/editOrder")
    @ResponseBody
    public List<Order> editOrder(@RequestBody Order order,Model model){
        //前端返回修改后的order数据，JSON形式
        //修改订单信息
        orderService.updateOrder(order);
        List<Order> orderList = orderService.getOrders();
        model.addAttribute("orderList", orderList);
        return orderList;
    }

    @RequestMapping(value="/order/searchOrder")
    @ResponseBody
    public List<Order> searchOrder(String searchContent,Model model){
        //前端返回searchContent字符串，用来匹配查询数据库
       List<Order> searchOrderList = orderService.getOrdersByUserId(searchContent);
        return searchOrderList;
    }

    //修改订单
    @PostMapping("/order/searchEditOrder")
    public void searchEditOrder(@RequestBody Order order){
        //前端返回修改后的order数据，JSON形式
        //修改订单信息
        orderService.updateOrder(order);
    }
}
